<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="年份" prop="year">
        <el-input
          v-model="queryParams.year"
          placeholder="请输入年份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="期号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入期号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户ID" prop="custId">
        <el-input
          v-model="queryParams.custId"
          placeholder="请输入客户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="玩法" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择玩法" clearable>
          <el-option
            v-for="dict in dict.type.play_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['financial:custOrder:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['financial:custOrder:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['financial:custOrder:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['financial:custOrder:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading"
              :data="custOrderList"
              @selection-change="handleSelectionChange"
              :row-class-name="resultClassName"
              default-expand-all="true"
    >
<!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column type="expand" align="center" fixed="left">
        <template slot-scope="props">
          <el-row :gutter="24">
            <el-col span="12" v-if="props.row.detainOrder">
              <el-card :body-style="{ padding: '10px' }">
                <div slot="header">
                  <span>收单信息</span>
                </div>
                <el-row :gutter="24">
                  <el-col :span="8">
                    <div>
                      <el-statistic
                        group-separator=","
                        :precision="2"
                        :value="props.row.detainOrder.amount"
                        title="金额"
                      ></el-statistic>
                    </div>
                  </el-col>
                  <el-col :span="8">
                    <div>
                      <el-statistic title="押/赔">
                        <template slot="formatter">
                          100/{{ props.row.detainOrder.odds * 100 }}
                        </template>
                      </el-statistic>
                    </div>
                  </el-col>
                  <el-col :span="8">
                    <div>
                      <el-statistic
                        group-separator=","
                        :precision="2"
                        decimal-separator="."
                        :value="props.row.detainOrder.waterRate"
                        title="退水比例"
                      />
                    </div>
                  </el-col>
                </el-row>
              </el-card>
            </el-col>
            <el-col span="12" v-if="props.row.outOrder">
              <el-card :body-style="{ padding: '10px' }">
                <div slot="header">
                  <span>出单信息</span>
                </div>
                <el-row :gutter="20">
                  <el-col :span="6">
                    <div>
                      <el-statistic
                        title="上家">
                        <template slot="formatter">
                          {{ props.row.outOrder.custName }}
                        </template>
                      </el-statistic>
                    </div>
                  </el-col>
                  <el-col :span="6">
                    <div>
                      <el-statistic
                        group-separator=","
                        :precision="2"
                        :value="props.row.outOrder.amount"
                        title="金额"
                      ></el-statistic>
                    </div>
                  </el-col>
                  <el-col :span="6">
                    <div>
                      <el-statistic title="押/赔">
                        <template slot="formatter">
                          100/{{ props.row.outOrder.odds * 100 }}
                        </template>
                      </el-statistic>
                    </div>
                  </el-col>
                  <el-col :span="6">
                    <div>
                      <el-statistic
                        group-separator=","
                        :precision="2"
                        decimal-separator="."
                        :value="props.row.outOrder.waterRate"
                        title="退水比例"
                      />
                    </div>
                  </el-col>
                </el-row>
              </el-card>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column label="玩法" align="center" prop="type" fixed="left">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.play_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="码" align="center" prop="codes" fixed="left" width="400">
        <template slot-scope="scope">
          <div class="codeClass" v-for="item in scope.row.codes.split(',')">{{ item }}</div>
        </template>
      </el-table-column>
      <el-table-column label="年份" align="center" prop="year"/>
      <el-table-column label="期号" align="center" prop="number"/>
      <el-table-column label="玩家" align="center" prop="custName"/>
      <el-table-column label="金额" align="center" prop="amount" sortable fixed="right"/>
      <el-table-column label="结算金额" align="center" prop="resultAmount" sortable fixed="right"/>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['financial:custOrder:edit']"-->
<!--          >修改-->
<!--          </el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['financial:custOrder:remove']"-->
<!--          >删除-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户押单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="年份" prop="year">
          <el-input v-model="form.year" placeholder="请输入年份"/>
        </el-form-item>
        <el-form-item label="期号" prop="number">
          <el-input v-model="form.number" placeholder="请输入期号"/>
        </el-form-item>
        <el-form-item label="码列表" prop="codes">
          <el-input v-model="form.codes" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="客户ID" prop="custId">
          <el-input v-model="form.custId" placeholder="请输入客户ID"/>
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID"/>
        </el-form-item>
        <el-form-item label="金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入金额"/>
        </el-form-item>
        <el-form-item label="玩法" prop="type">
          <el-select v-model="form.type" placeholder="请选择玩法">
            <el-option
              v-for="dict in dict.type.play_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="${comment}" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入${comment}"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCustOrder, getCustOrder, delCustOrder, addCustOrder, updateCustOrder} from "@/api/financial/custOrder";

export default {
  name: "CustOrder",
  dicts: ['play_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户押单表格数据
      custOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        year: null,
        number: null,
        codes: null,
        custId: null,
        userId: null,
        amount: null,
        type: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        year: [
          {required: true, message: "年份不能为空", trigger: "blur"}
        ],
        number: [
          {required: true, message: "期号不能为空", trigger: "blur"}
        ],
        codes: [
          {required: true, message: "码列表不能为空", trigger: "blur"}
        ],
        custId: [
          {required: true, message: "客户ID不能为空", trigger: "blur"}
        ],
        userId: [
          {required: true, message: "用户ID不能为空", trigger: "blur"}
        ],
        amount: [
          {required: true, message: "金额不能为空", trigger: "blur"}
        ],
        type: [
          {required: true, message: "玩法不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    resultClassName({row, rowIndex}) {
      if (row.resultAmount > 0) {
        return 'success-row';
      } else if (row.resultAmount < 0) {
        return 'warning-row';
      }
      return '';
    },
    /** 查询客户押单列表 */
    getList() {
      this.loading = true;
      listCustOrder(this.queryParams).then(response => {
        this.custOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        year: null,
        number: null,
        codes: null,
        custId: null,
        userId: null,
        amount: null,
        type: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户押单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCustOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户押单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCustOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户押单编号为"' + ids + '"的数据项？').then(function () {
        return delCustOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('financial/custOrder/export', {
        ...this.queryParams
      }, `custOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss">
.like {
  cursor: pointer;
  font-size: 25px;
  display: inline-block;
}

.codeClass {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: #FF5722;
  text-align: center;
  line-height: 50px;
  font-size: 16px;
  font-weight: bold;
  color: white;
  margin-left: 5px;
  display: inline-block;
}

 .el-table .warning-row {
   background: oldlace;
 }

.el-table .success-row {
  background: #f0f9eb;
}
</style>
